package com.yanliang.SqlSession;

import com.yanliang.config.XMLConfigerBuilder;
import com.yanliang.pojo.Configuration;
import org.dom4j.DocumentException;

import java.beans.PropertyVetoException;
import java.io.InputStream;

/**
 * 解析配置文件，返回SqlSessionFactory$
 *
 * @Author : yanliang
 * @Data : 3/25/2020 5:40 PM
 */
public class SqlSessionFactoryBuilder {

    private Configuration configuration;

    public SqlSessionFactoryBuilder() {
        configuration = new Configuration();
    }

    public SqlSessionFactory build(InputStream inputStream) throws DocumentException, PropertyVetoException, ClassNotFoundException {
        //1. 解析配置文件，封装Configuration
        XMLConfigerBuilder xmlConfigerBuilder = new
                XMLConfigerBuilder(configuration);

        Configuration configuration =
                xmlConfigerBuilder.parseConfiguration(inputStream);

        //2. 创建sqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new
                DefaultSqlSessionFactory(configuration);

        return sqlSessionFactory;
    }
}
